# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
import logging
from scrapy.utils.project import get_project_settings
from scrapy.exceptions import DropItem

settings = get_project_settings()


class JobPipeline(object):
    def __init__(self):
        host = settings["MONGODB_HOST"]
        port = settings["MONGODB_PORT"]
        dbname = settings["MONGODB_DB"]
        collection = settings["MONGODB_COLLECTION"]
        logging.info(f"Get settings {host} {port} {dbname} {collection}")
        # 创建MONGODB数据库链接
        client = pymongo.MongoClient(host=host, port=port)
        # 指定数据库
        mydb = client[dbname]
        # 存放数据的数据库表名
        self.post = mydb[collection]

    def process_item(self, item, spider):
        valid = True
        for data in item:
            if not data:
                valid = False
                raise DropItem("Missing {0}!".format(data))
        if valid:
            self.post.insert(dict(item))
            logging.info("Post added to MongoDB database!")
        return item


if __name__ == '__main__':
    pipe = JobPipeline()
